package edu.whut.mall.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import edu.whut.mall.admin.domain.MerchantParams;
import edu.whut.mall.admin.domain.MerchantSearchParams;
import edu.whut.mall.admin.domain.UserParams;
import edu.whut.mall.admin.enums.MerchantSettledStatusEnum;
import edu.whut.mall.admin.mapper.MerchantCustomMapper;
import edu.whut.mall.admin.mapper.UserCustomMapper;
import edu.whut.mall.admin.service.IMerchantService;
import edu.whut.mall.admin.service.IProductService;
import edu.whut.mall.admin.service.ISysUserService;
import edu.whut.mall.admin.vo.MerchantVo;
import edu.whut.mall.admin.vo.UserVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.mapper.MerchantMapper;
import edu.whut.mall.mapper.MerchantSysUserMapper;
import edu.whut.mall.model.Merchant;
import edu.whut.mall.model.MerchantExample;
import edu.whut.mall.model.MerchantSysUser;
import edu.whut.mall.model.SysUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: MerchantService
 * @Description: 商家相关服务实现类
 * @Author: lvfanbing
 * @Date: 2020-10-12 17:21
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class MerchantServiceImpl implements IMerchantService {

    private final MerchantMapper merchantMapper;
    private final IProductService productService;
    private final MerchantSysUserMapper merchantSysUserMapper;
    private final MerchantCustomMapper merchantCustomMapper;
    private final UserCustomMapper userCustomMapper;
    private final ISysUserService sysUserService;

    @Override
    public int insertOneMerchant(MerchantParams merchantParams) {
        Merchant merchant = new Merchant();
        merchant.setCreateTime(new Date());
        merchant.setUpdateTime(new Date());
        BeanUtil.copyProperties(merchantParams, merchant);
        merchant.setMerchantStatus(MerchantSettledStatusEnum.IN_REVIEW.getCode());
        return merchantMapper.insertSelective(merchant);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteOneMerchant(Long merchantId) {
        productService.deleteProductByMerchantId(merchantId);
        userCustomMapper.deleteUserByMerchantId(merchantId);
        Merchant merchant = new Merchant();
        merchant.setUpdateTime(new Date());
        merchant.setIsDeleted(1);
        MerchantExample merchantExample = new MerchantExample();
        merchantExample.createCriteria().andIdEqualTo(merchantId);
        return merchantMapper.updateByExampleSelective(merchant, merchantExample);
    }

    @Override
    public int updateOneMerchant(Long merchantId, MerchantParams merchantParams) {
        Merchant merchant = new Merchant();
        merchant.setUpdateTime(new Date());
        BeanUtil.copyProperties(merchantParams, merchant);
        MerchantExample merchantExample = new MerchantExample();
        merchantExample.createCriteria().andIdEqualTo(merchantId);
        return merchantMapper.updateByExampleSelective(merchant, merchantExample);
    }

    @Override
    public int updateOneMerchantSettledStatus(Long merchantId, Integer status) {
        Merchant merchant = new Merchant();
        merchant.setMerchantStatus(status);
        merchant.setUpdateTime(new Date());
        MerchantExample merchantExample = new MerchantExample();
        merchantExample.createCriteria().andIdEqualTo(merchantId);
        return merchantMapper.updateByExampleSelective(merchant, merchantExample);
    }

    @Override
    public int assignAdminToMerchant(Long merchantId, String regIp, UserParams userParams) {
        SysUser sysUser = sysUserService.insertOneUser(regIp, userParams);
        MerchantSysUser merchantSysUser = new MerchantSysUser();
        merchantSysUser.setMerchantId(merchantId);
        merchantSysUser.setSysUserId(sysUser.getId());
        return merchantSysUserMapper.insertSelective(merchantSysUser);
    }

    @Override
    public int setProceedsFlowDirection(Long merchantId, Integer direction) {
        Merchant merchant = new Merchant();
        merchant.setProceedsFlowDirection(direction);
        merchant.setUpdateTime(new Date());
        MerchantExample merchantExample = new MerchantExample();
        merchantExample.createCriteria().andIdEqualTo(merchantId);
        return merchantMapper.updateByExampleSelective(merchant, merchantExample);
    }

    @Override
    public CommonPage<MerchantVo> listAllMerchant(Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum, size);
        List<MerchantVo> merchantList = merchantCustomMapper.listAllMerchantVos();
        return CommonPage.restPage(merchantList);
    }

    @Override
    public CommonPage<MerchantVo> listMerchantByCondition(MerchantSearchParams merchantSearchParams, Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum, size);
        List<MerchantVo> merchantList = merchantCustomMapper.listMerchantVosByCondition(merchantSearchParams);
        return CommonPage.restPage(merchantList);
    }

    @Override
    public List<UserVo> listAllAdminByMerchantId(Long merchantId) {
        return userCustomMapper.listUserVosByMerchantId(merchantId);
    }
}
